package com.example.mq.mqserver.core;

import java.io.Serializable;
import java.util.Arrays;
import java.util.UUID;


//要传递的消息
//需要在网络上传输，需要进行序列化，反序列化 用标准库实现Serializable,这个接口实现之后就可以了，不用重写方法
//json本质是文本数据 ，这里message是二进制数据
public class Message implements Serializable {

    //用于验证版本 因为如果针对不同版本的数据进行反序列化，大概会失败（数据可能被更改过）
    private final static long serialVersionUID = 1L;

    private BasicProperties basicProperties = new BasicProperties();
    private byte[] body;

    //下面是辅助用的属性
    //用来定位消息数据的位置[offsetBeg,offsetEnd)
    //下面两个属性不需要序列化
    //表示该消息是否在文件中是 有效消息（逻辑删除）
    private transient long offsetBeg = 0;
    private transient long offsetEnd = 0;


    //0x1无效 0x0有效
    private byte isValid = 0x1;


    //自动生成ID
    public static Message createMessageWithId(String routingKey, BasicProperties basicProperties, byte[] body) {
        Message message = new Message();
        if (basicProperties != null) {

            message.setBasicProperties(basicProperties);

        }

        //ID用M做前缀，表示消息的ID
        message.setMessageId("M-" + UUID.randomUUID());
        message.setRoutingKey(routingKey);
        message.setBody(body);
        return message;
    }


    public String getMessageId() {
        return basicProperties.getMessageId();
    }

    public void setMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }

    public int getDeliverMode() {
        return basicProperties.getDeliveryMode();
    }

    public void setDeliverMode(int mode) {
        basicProperties.setDeliveryMode(mode);
    }


    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }

    @Override
    public String toString() {
        return "Message{" +
                "basicProperties=" + basicProperties +
                ", body=" + Arrays.toString(body) +
                ", offsetBeg=" + offsetBeg +
                ", offsetEnd=" + offsetEnd +
                ", isValid=" + isValid +
                '}';
    }
}
